package com.wjk.kylin.modules.system.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.log.entity.OperateLog;
import com.wjk.kylin.modules.system.log.service.ILogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 *  日志 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-05
 */
@Api("日志")
@RestController
@RequestMapping("/sys/log")
public class LogController extends BaseController {

    @Autowired
    private ILogService logService;

    @PreAuthorize("hasAuthority('system:log:list')")
    @ApiOperation("列表")
    @Log(title = "日志-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(OperateLog operateLog) {
        IPage<OperateLog> page = logService.findPage(new RequestData<OperateLog>(), operateLog);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:log:query')")
    @ApiOperation("详情")
    @Log(title = "日志-详情",businessType = GET)
    @GetMapping("/{operateLogId}")
    public ResponseData get(@PathVariable String operateLogId) {
        OperateLog operateLog = logService.getById(operateLogId);
        return ResponseData.success().data("operateLog", operateLog);
    }

    @PreAuthorize("hasAuthority('system:log:delete')")
    @ApiOperation("删除")
    @Log(title = "日志-删除",businessType = DELETE)
    @DeleteMapping("/{operateLogIds}")
    public ResponseData delete(@PathVariable String[] operateLogIds) {

        boolean flag = logService.deleteByIds(operateLogIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @PreAuthorize("hasAuthority('system:log:delete')")
    @ApiOperation("清空")
    @Log(title = "日志-清空",businessType = CLEAN)
    @DeleteMapping("/clean")
    public ResponseData clean() {
        logService.clean();
        return ResponseData.success();
    }
}
